package com.example.facechecklibrary.View;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.example.facechecklibrary.Interface.FaceAppear;
import com.faceplusplus.api.FaceDetecter.Face;

public class FaceMask extends View {

    private Paint localPaint;
    private Face[] faceInfos;
    private RectF rect;
    private static FaceAppear faceAppear;

    public FaceMask(Context context, AttributeSet atti) {
        super(context, atti);
        rect = new RectF();
        localPaint = new Paint();
        localPaint.setColor(0xff248FAF);
        localPaint.setStrokeWidth(5);
        localPaint.setStyle(Paint.Style.STROKE);
    }

    public void setFaceInfo(Face[] faceInfos) {
        this.faceInfos = faceInfos;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (faceInfos == null) {
            faceAppear.faceAppear(false);
            return;
        }
        //这里是绘制所有的人脸
        for (Face localFaceInfo : faceInfos) {
            rect.set(getWidth() * localFaceInfo.left - 100, getHeight() * localFaceInfo.top - 100,
                    getWidth() * localFaceInfo.right + 100, getHeight() * localFaceInfo.bottom + 100);
            canvas.drawRect(rect, localPaint);
        }
        //只是绘制第一个人脸
//        if (faceInfos[0] != null) {
//            rect.set(getWidth() * faceInfos[0].left - 100, getHeight() * faceInfos[0].top - 100,
//                    getWidth() * faceInfos[0].right + 100, getHeight() * faceInfos[0].bottom + 100);
//            canvas.drawRect(rect, localPaint);
//        }

        //如果有框 告诉外面 人脸出现
        faceAppear.faceAppear(true);
    }

    public static void setFaceAPPearListener(FaceAppear faceAPPearListener) {
        faceAppear = faceAPPearListener;
    }
}
